package com.ebook.server.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.ebook.core.domain.User;

@Controller
@RequestMapping(value = "/")
public class HomepageController {

	@RequestMapping(method = RequestMethod.GET)
//	@RequestMapping("")
	public String index(Model model, HttpServletRequest request) {
		//TODO: add more
		return "/home/index";
	}

	@RequestMapping("/books.htm")
	public String showBooks(Model model, HttpServletRequest request) {

		return "/book/list";
	}

	@RequestMapping("/user/{uid}.htm")
	public String showUserInfo(@PathVariable int uid, Model model, HttpServletRequest request, HttpServletResponse response) {
		User user = (User)request.getSession().getAttribute("loginuser");

		if (user == null || user.getId() != uid) {
			model.addAttribute("error", true);
			model.addAttribute("errormsg", "用户不存在或您没有权限查看！");
			return "/common/error";
		}
		return "/user/userinfo";
	}
}
